// pages/shopThird/shopThird.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '', // 页面标题
    menuId: '', // 商品列表id
    bannerList: [],
    actionList: [],
    menuList: [],
    menuFlag: 0,
    shopList: [],
    searchFlag: false,
    changeTypeFlag: false,
    noList: false,
    queryType: '', // 排序方式 1 最新倒序 2最新顺序 3价格高低 4 价格低高 5 销量高低 6 销量低高
    pageNum: 1, // 分页参数
    timeFlag: 'none',
    priceFlag: 'none',
    numFlag: 'none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.info("==== options ====", options)
    // this.setData({
    //   menuId: options.id,
    //   title: options.name
    // })
    this.getList()
  },
  
  // 跳转商品详情
  toDetail(e) {
    if(!e.currentTarget.dataset['type']) {
      wx.navigateTo({
        url: '/pages/shopDetail/shopDetail?id=' + e.currentTarget.dataset['id']
      })
      return
    }
    if(e.currentTarget.dataset['type'] && e.currentTarget.dataset['type'] == 2) {
      wx.navigateTo({
        url: '/pages/shopDetail/shopDetail?id=' + e.currentTarget.dataset['id']
      })
    }
  },

  // 跳转购物车
  toShopCar() {
    wx.navigateTo({
      url: '/pages/shopCar/shopCar',
    })
  },
  
  // 获取首页数据
  getList() {
    // tab列表 轮播列表 商品列表
    Promise.all([wx.promiseGet('/api/banner/mallList'), wx.promiseGet('/api/mallCategory/l1')], ).then(res=>{
      this.setData({
        loading: false,
        bannerList: res[0].data.banner,
        actionList: res[0].data.action,
        menuList: res[1].categoryS,
        tabFlag: res[1].categoryS[0].id,
      })
      this.getShopList()
    })
  },

  // 商品列表
  getShopList() {
    // let api = `/api/goods/mallList?categoryId=${this.data.menuId}&pageNum=${this.data.pageNum}`
    let api = `/api/goods/mallList?&pageNum=${this.data.pageNum}`
    if (this.data.tabFlag) api += `&categoryId=${this.data.tabFlag}`
    // if (this.data.searchFlag) api += `&name=${this.data.value}`
    // if (this.data.queryType) api += `&queryType=${this.data.queryType}`
    wx.get(api, res => {
      console.log('==== shopList ===', res)
      if (res.rows.length > 0) {
        res.rows = res.rows.map(item=> {
          return {
            ...item,
            totalAmount: (item.totalAmount*100 + item.dummyMoney*100)/100
          }
        })
        this.setData({
          shopList: this.data.shopList.concat(res.rows)
        })  
      } else {
        if (this.data.pageNum > 1) {
          this.setData({
            noList: true
          })
        }
      }
      if (this.data.changeTypeFlag == true) {
        this.setData({
          changeTypeFlag: false
        })
      }
    }, fail => {})
  },

  toSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  
  onChange(e) {
    this.setData({
      value: e.detail,
    });
    if (e.detail == '') {
      this.setData({
        pageNum: 1,
        searchFlag: false,
        shopList: []
      })
      this.getShopList()
    }
  },

  onSearch() {
    console.log('搜索' + this.data.value);
    this.setData({
      pageNum: 1,
      noList: false,
      searchFlag: true,
      shopList: []
    })
    this.getShopList()
  },

  onClear() {
    console.log('==== 清空搜索 ====', this.data.value);
  },

  tabClick(e) {
    let id = e.currentTarget.dataset.item.id
    this.setData({
      tabFlag: id,
      pageNum: 1,
      noList: false,
      shopList: []
    })
    this.getShopList()
  },
  
  // 切换排序方式
  changeType(e) {
    let clickType = e.currentTarget.dataset.type
    this.setData({
      pageNum: 1,
      noList: false,
      changeTypeFlag: true,
      shopList: []
    })
    if (clickType == '1') {
      let timeFlag = ''
      let queryType = ''
      if (this.data.timeFlag == 'up') {
        timeFlag = 'down'
        queryType = '1'
      } else {
        timeFlag = 'up'
        queryType = '2'
      }
      this.setData({
        timeFlag,
        queryType,
        priceFlag: 'none',
        numFlag: 'none'
      })
    } else if (clickType == '2') {
      let priceFlag = ''
      let queryType = ''
      if (this.data.priceFlag == 'up') {
        priceFlag = 'down'
        queryType = '3'
      } else {
        priceFlag = 'up'
        queryType = '4'
      }
      this.setData({
        priceFlag,
        queryType,
        timeFlag: 'none',
        numFlag: 'none'
      })
    } else if (clickType == '3') {
      let numFlag = ''
      let queryType = ''
      if (this.data.numFlag == 'up') {
        numFlag = 'down'
        queryType = '5'
      } else {
        numFlag = 'up'
        queryType = '6'
      }
      this.setData({
        numFlag,
        queryType,
        timeFlag: 'none',
        priceFlag: 'none'
      })
    }
    this.getShopList()
  },

  taskLower() {
    console.info('==== 触底 ====')
    if (this.data.noList) return
    if (this.data.changeTypeFlag == true) return
    this.data.pageNum ++
    this.getShopList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})